﻿<ResourceDictionary 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:System.Controls">

  <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Common.xaml"/>
  </ResourceDictionary.MergedDictionaries>

  <ControlTemplate x:Key="controlTemplate" TargetType="{x:Type local:GroupBox}">
    <Grid>
      <Grid.ColumnDefinitions>
        <ColumnDefinition x:Name="left" Width="{Binding Path=BorderThickness, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource gridLengthConverter}, ConverterParameter='1,0,0,0'}"/>
        <ColumnDefinition x:Name="headerCol" Width="Auto"/>
        <ColumnDefinition x:Name="contentCol"/>
        <ColumnDefinition x:Name="right" Width="{Binding Path=BorderThickness, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource gridLengthConverter}, ConverterParameter='0,0,1,0'}"/>
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
        <RowDefinition x:Name="top" Height="Auto"/>
        <RowDefinition x:Name="headerRow" Height="Auto"/>
        <RowDefinition x:Name="contentRow"/>
        <RowDefinition x:Name="bottom" Height="{Binding Path=BorderThickness, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource gridLengthConverter}, ConverterParameter='0,0,0,1'}"/>
      </Grid.RowDefinitions>

      <Border 
        Grid.Column="0" 
        Grid.Row="1" 
        Grid.ColumnSpan="4" 
        Grid.RowSpan="3"
        BorderBrush="Transparent" 
        CornerRadius="{Binding Path=CornerRadius, RelativeSource={RelativeSource TemplatedParent}}" 
        BorderThickness="{Binding Path=BorderThickness, RelativeSource={RelativeSource TemplatedParent}}" 
        Background="{TemplateBinding Background}" />

      <Border 
        x:Name="border"
        Grid.Column="0"
        Grid.Row="1" 
        Grid.RowSpan="3"
        Grid.ColumnSpan="4" 
        CornerRadius="{TemplateBinding CornerRadius}"
        BorderBrush="{TemplateBinding BorderBrush}" 
        BorderThickness="{TemplateBinding BorderThickness}" >

        <Border.OpacityMask>
          <VisualBrush>
            <VisualBrush.Visual>
              <Grid 
                Width="{Binding ElementName=border, Path=ActualWidth}" 
                Height="{Binding ElementName=border, Path=ActualHeight}">
                <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="{Binding Path=BorderThickness, RelativeSource={RelativeSource TemplatedParent},Converter={StaticResource gridLengthConverter}, ConverterParameter='*2,0,0,0'}"/>
                  <ColumnDefinition Width="{Binding ElementName=Header, Path=ActualWidth}"/>
                  <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                  <RowDefinition Height="{Binding ElementName=Header, Path=ActualHeight}"/>
                  <RowDefinition />
                </Grid.RowDefinitions>

                <Rectangle Fill="Black" Grid.RowSpan="2"/>
                <Rectangle Fill="Black" Grid.Column="1" Grid.Row="1"/>
                <Rectangle Fill="Black" Grid.Row="0" Grid.RowSpan="2" Grid.Column="2"/>
              </Grid>
            </VisualBrush.Visual>
          </VisualBrush>
        </Border.OpacityMask>

      </Border>

      <Border 
        x:Name="Header" 
        Grid.Row="0" 
        Grid.Column="1" 
        Grid.RowSpan="2" 
        Padding="5,0" 
        Background="{TemplateBinding Background}"
        CornerRadius="{Binding Path=CornerRadius,RelativeSource={RelativeSource TemplatedParent} ,Converter={StaticResource cornerRadius}, ConverterParameter='1,1,0,0'}"
        Margin="{Binding Path=BorderThickness, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource thicknes}, ConverterParameter='1,0,0,0'}">
        <ContentPresenter  ContentSource="Header" RecognizesAccessKey="True" SnapsToDevicePixels="True"/>
      </Border>

      <ContentPresenter 
        Grid.ColumnSpan="2" 
        Grid.Column="1" 
        Grid.Row="2" 
        ContentSource="Content"
        Margin="{TemplateBinding Padding}" />
    </Grid>

  </ControlTemplate>

  <Style TargetType="{x:Type local:GroupBox}" BasedOn="{StaticResource {x:Type Control}}">
    <Setter Property="BorderThickness" Value="2"/>
    <Setter Property="CornerRadius" Value="5"/>
    <Setter Property="BorderBrush" Value="#C2A579"/>
    <Setter Property="Foreground"  Value="#FFE0E8D9"/>
    <Setter Property="Background" Value="#FF83550E"/>
    <Setter Property="Template" Value="{StaticResource controlTemplate}"/>
  </Style>

</ResourceDictionary>